<!-- 账户管理 -->
<template>
  <div class="whole">
    <el-container>
      <el-aside width="200px">
        <CommonAside />
      </el-aside>
      <el-container>
        <el-header>
          <ComponentsHeader />
        </el-header>
        <el-main>
          <div class="app-container">
            <!-- 搜索 -->
            <el-form
              :model="queryParams"
              ref="queryForm"
              size="small"
              :inline="true"
              v-show="showSearch"
              label-width="100px"
              class="mag"
            >
              <el-form-item label="员工编号" prop="employeeNumber">
                <el-input
                  v-model="queryParams.employeeNumber"
                  placeholder="请输入员工编号"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="姓名" prop="name">
                <el-input
                  v-model="queryParams.name"
                  placeholder="请输入姓名"
                  clearable
                />
              </el-form-item>
              <el-form-item>
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  size="mini"
                  @click="handleQuery"
                  >搜索</el-button
                >
                <el-button
                  icon="el-icon-refresh"
                  size="mini"
                  @click="resetQuery"
                  >重置</el-button
                >
              </el-form-item>
            </el-form>

            <el-row :gutter="10" class="mb8" style="margin-bottom: 8px">
              <el-col :span="1.5">
                <el-button
                  type="primary"
                  plain
                  icon="el-icon-plus"
                  size="mini"
                  @click="handleAdd"
                  >新增</el-button
                >
                <el-button
                  type="success"
                  plain
                  icon="el-icon-edit"
                  size="mini"
                  :disabled="single"
                  @click="handleUpdata"
                  >修改</el-button
                >
                <el-button
                  type="danger"
                  plain
                  icon="el-icon-delete"
                  size="mini"
                  :disabled="multiple"
                  @click="handleDelete"
                  >删除
                </el-button>
              </el-col>

              <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
            </el-row>
            <!-- 账户表格 -->
            <el-table
              v-loading="loading"
              :data="accountList"
              @selection-change="handleSelectionChange"
            >
              <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="id" align="center" prop="id" />
              <el-table-column
                label="员工编号"
                align="center"
                prop="employeeNumber"
              />
              <el-table-column label="角色" align="center" prop="role" />
              <el-table-column label="姓名" align="center" prop="name" />
              <el-table-column
                label="部门"
                align="center"
                prop="department.name"
              />
              <el-table-column label="职位" align="center" prop="job" />
              <el-table-column label="电话" align="center" prop="phone" />
              <el-table-column label="头像" align="center" prop="pic">
                <template slot-scope="scope">
                  <img :src="scope.row.pic" />
                </template>
              </el-table-column>
              <el-table-column label="描述" align="center" prop="remarks" />
              <el-table-column
                label="编辑时间"
                align="center"
                prop="createTime"
              />
              <el-table-column
                label="最后编辑时间"
                align="center"
                prop="updateTime"
              />
              <el-table-column
                label="操作"
                align="center"
                class-name="small-padding fixed-width"
              >
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleUpdata(scope.row)"
                    >修改
                  </el-button>
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleDelete(scope.row)"
                    >删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <!--            //分页-->
            <div class="block" style="float: right; margin-top: 24px">
              <el-pagination
                v-show="total > 0"
                background
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="queryParams.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
              >
              </el-pagination>
            </div>
            <!--    <pagination-->
            <!--        v-show="total>0"-->
            <!--        :total="total"-->
            <!--        :page.sync="queryParams.pageNum"-->
            <!--        :limit.sync="queryParams.pageSize"-->
            <!--        @pagination="getList"-->
            <!--    />-->

            <!-- 添加或修改智造双碳-双碳配置-固定碳排放对话框 -->
            <el-dialog
              :title="title"
              :visible.sync="accountopen"
              width="500px"
              append-to-body
            >
              <el-form
                ref="form"
                :model="form"
                :rules="rules"
                label-width="100px"
              >
                <!-- <el-form-item label="id" prop="id" style="display: none">
                  <el-input v-model="form.id"></el-input>
                </el-form-item> -->
                <el-form-item label="员工编号" prop="employeeNumber">
                  <el-input
                    v-model="form.employeeNumber"
                    placeholder="请输入员工编号"
                  />
                </el-form-item>
                <el-form-item label="角色" prop="role">
                  <el-input v-model="form.role" placeholder="请输入角色" />
                </el-form-item>
                <el-form-item label="姓名" prop="name">
                  <el-input v-model="form.name" placeholder="请输入姓名" />
                </el-form-item>
                <el-form-item label="部门" prop="department.id">
                  <el-input
                    v-if="form.department"
                    v-model="form.department.name"
                    placeholder="请选择"
                    disabled
                  >
                    <el-button slot="append" @click="openDialog"
                      >选择</el-button
                    >
                  </el-input>
                </el-form-item>
                <el-form-item label="职位" prop="job">
                  <el-input v-model="form.job" placeholder="请输入职位" />
                </el-form-item>
                <el-form-item label="电话" prop="phone">
                  <el-input v-model="form.phone" placeholder="请输入电话" />
                </el-form-item>
                <el-form-item label="头像" prop="pic">
                  <!-- file-list :上传的文件列表 before-remove移除上传的照片 -->
                  <el-upload
                    action="#"
                    list-type="picture-card"
                    :auto-upload="false"
                  >
                    <i slot="default" class="el-icon-plus"></i>
                  </el-upload>
                </el-form-item>
                <el-form-item label="描述" prop="remarks">
                  <el-input v-model="form.remarks" placeholder="请输入描述" />
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
              </div>
            </el-dialog>
            <!-- 选择部门弹框 -->
            <el-dialog title="选择部门" :visible.sync="depaertopen">
              <!-- 搜索 -->
              <el-form
                :model="queryParams"
                ref="queryForm"
                size="small"
                :inline="true"
                v-show="showSearch"
                label-width="100px"
              >
                <el-form-item label="部门名称" prop="name">
                  <el-input
                    v-model="queryParams.name"
                    placeholder="请输入部门名称"
                    clearable
                  />
                </el-form-item>

                <el-form-item>
                  <el-button
                    type="primary"
                    icon="el-icon-search"
                    size="mini"
                    @click="handleQuery"
                    >搜索</el-button
                  >
                  <el-button
                    icon="el-icon-refresh"
                    size="mini"
                    @click="resetQuery"
                    >重置</el-button
                  >
                </el-form-item>
              </el-form>
              <!-- 表格 -->
              <el-table
                v-if="refreshTable"
                v-loading="loading"
                :default-expand-all="isExpandAll"
                row-key="id"
                :data="departmentList"
                :tree-props="{
                  children: 'children',
                  hasChildren: 'hasChildren',
                }"
              >
                <el-table-column label="部门名称" prop="name" width="180px" />
                <el-table-column
                  label="用户人数"
                  prop="userNumber"
                  label-width="80px"
                />
                <el-table-column label="排序" align="center" prop="sort" />
                <el-table-column
                  label="编辑时间"
                  align="center"
                  prop="createTime"
                />
                <el-table-column
                  label="更新时间"
                  align="center"
                  prop="updateTime"
                />
                <el-table-column
                  label="操作"
                  align="center"
                  class-name="small-padding fixed-width"
                >
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="closedate(scope.row)"
                      >选择
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
            </el-dialog>
          </div>
        </el-main>
      </el-container>
    </el-container>
    <!-- 调用子组件 -->
  </div>
</template>

<script>
import {
  listAccount,
  getAccount,
  delAccount,
  addAccount,
  updateAccount,
  uploadPhoto,
} from "@/api/AccountManagement";
import CommonAside from "@/components/CommonAside.vue";
import { listTreeDepartment } from "@/api/DepartmentManagement";
import ComponentsHeader from "@/components/ComponentsHeader.vue";

export default {
  name: "Account",
  components: { ComponentsHeader, CommonAside },
  data() {
    return {
      // 上传图片
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
      // 遮罩层
      loading: false,
      // 选择部门弹框
      depaertopen: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 是否展开子节点
      isExpandAll: true,
      // 弹框部门表格数据
      departmentList: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      accountList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      accountopen: false,
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 10,
        employeeNumber: null,
        name: null,
      },
      // 表单参数
      form: {
        department: {},
        employeeNumber: " ",
      },
      // 表单校验
      rules: {
        employeeNumber: [
          { required: true, message: "员工编号不能为空", trigger: "blur" },
          {
            min: 2,
            max: 30,
            message: "长度在 2 到 30 个字符",
            trigger: "blur",
          },
        ],
        name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
    this.getList2();
  },
  methods: {
    /** 查询账户列表 */
    getList() {
      this.loading = true;
      listAccount(this.queryParams).then((response) => {
        console.log(response);
        this.accountList = response.data.data.data;
        this.total = response.data.data.totalRecords;
        this.loading = false;
        console.log(this.accountList);
      });
    },
    /** 弹框查询部门列表 */
    getList2() {
      this.loading = true;
      console.log();
      listTreeDepartment(this.queryParams).then((response) => {
        this.departmentList = response.data.data;
        console.log(response);
      });
      this.loading = false;
    },
    // 取消按钮
    cancel() {
      this.accountopen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name: null,
        id: null,
        employeeNumber: null,
        role: null,
        job: null,
        phone: null,
        remarks: null,
        pic: null,
      };
      this.$refs["queryForm"].resetFields();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs["queryForm"].resetFields();
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.accountopen = true;
      this.title = "新增账户";
      this.form = {
        department: {},
      };
    },
    /** 修改按钮操作 */
    handleUpdata(row) {
      this.reset();
      const id = row.id || this.ids;
      getAccount(id).then((response) => {
        this.form = response.data.data;
        console.log();
        this.accountopen = true;
        this.title = "修改账户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAccount(this.form).then((response) => {
              console.log(response);
              this.$message("修改成功");
              this.accountopen = false;
              this.getList();
            });
          } else {
            addAccount(this.form).then((response) => {
              console.log(response);
              this.$message({
                type: "success",
                message: "新增成功!",
              });
              this.accountopen = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除"' + ids + '"的数据项？').then(function() {
        return delAccount(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    //分页相关方法
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.queryParams.page = val;
      this.getList();
    },
    // 选择按钮弹窗
    openDialog() {
      this.depaertopen = true;
    },
    //弹框部门列表子节点折叠或者打开
    toggleExpandAll() {
      this.isExpandAll = !this.isExpandAll; //切换子菜单
      // 需要表格重新绘制一下，否则会产生固定列位错误
      this.$nextTick(() => {
        this.refreshTable = true; //展开子菜单
      });
    },
    // 弹框部门选择按钮
    closedate(val) {
      this.depaertopen = false;
      this.form.department.id = val.id;
      this.form.department.name = val.name;
    },
    //
    handleRemove(file) {
      console.log(file);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleDownload(file) {
      console.log(file);
    },
  },
};
</script>
<style scoped>
.mag {
  margin-left: -30px;
}
.input[type="file"] {
    display: block;
}
</style>